package net.pimpas.core.visitor;

import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;

import net.pimpas.util.EqualsHelper;

import org.apache.commons.lang.builder.HashCodeBuilder;

/**
 * 
 * @author pc
 * Create a sub-class of this class to visit classes int ClassPath on BootStrap.
*/
public abstract class BaseClassVisitor implements ClassVisitor {
	
	private Collection<Class<?>> allowedClasses;
	private boolean include;
	
	public BaseClassVisitor() {
		this.setAllowedClasses(new HashSet<Class<?>>());
	}
	
	public BaseClassVisitor(boolean include, Class<?>... classes) {
		this();
		this.setInclude(include);
		this.getAllowedClasses().addAll(Arrays.asList(classes));
	}
	
	public boolean accept(Class<?> clazz) {
		return (this.getAllowedClasses().size() == 0 || 
				(this.isInclude()) ? this.getAllowedClasses().contains(clazz) : !this.getAllowedClasses().contains(clazz));
	}

	public Collection<Class<?>> getAllowedClasses() {
		return allowedClasses;
	}

	public void setAllowedClasses(Collection<Class<?>> allowedClasses) {
		this.allowedClasses = allowedClasses;
	}

	public boolean isInclude() {
		return include;
	}

	public void setInclude(boolean include) {
		this.include = include;
	}
	
	@Override
	public int hashCode() {
		return new HashCodeBuilder()
			.append(this.isInclude())
			.append(this.getAllowedClasses()).toHashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		return EqualsHelper.equals(this, obj);
	}
}